module.exports = class Chain {
    constructor(fn) {
        this.fn = fn;
        this.successor = null;
    }

    setNextSuccessor(successor) {
        return this.successor = successor;
    }

    passRequest(...args) {
        let ret = this.fn.apply(this, args);
        if (ret === 'nextSuccessor') {
            return this.successor && this.successor.passRequest.apply(this.successor, args);
        }
        return ret;
    }

    next(...args) {
        return this.successor && this.successor.passRequest.apply(this.successor, args);
    }
}